package com.daocao.hua.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.daocao.hua.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-6-1.
 */

public class DialogActivity extends AppCompatActivity {
    @BindView(R.id.webview)
    FrameLayout mWebContainer;
    @BindView(R.id.btn_cancel)
    TextView btnCancel;
    private WebView mWebView;
    private Unbinder unbiner;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setGravity(Gravity.CENTER);
        unbiner = ButterKnife.bind(this);

        mWebView = new WebView(getApplicationContext());
        mWebContainer.addView(mWebView);
        String url = getIntent().getStringExtra("url");
        initWebView(url);
    }

    private void initWebView(String url) {
        mWebContainer.setVisibility(View.VISIBLE);
        mWebView.setBackgroundColor(0);
        if (Build.VERSION.SDK_INT >= 21) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
//        mWebView.setWebChromeClient(new WebChromeClient());
        // // 设置可以支持缩放
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebContainer.removeAllViews();
        if (mWebView != null) {
            mWebView.onPause();
            mWebView.destroy();
            mWebView = null;
        }
        unbiner.unbind();
        unbiner = null;

    }

    @OnClick(R.id.btn_cancel)
    public void onViewClicked() {
        finish();
    }
}
